package com.dudu.timer;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.model.PushAccount;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.IAppExtendService;
import com.dudu.service.impl.AppExtendServiceImpl;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.HttpClientCenter;
import com.dudu.utils.HttpRequest;

/**
 * 线程把accounts进行处理 1、更新主cust_account的几个URL，把副账号的URL送到主的上。
 * 2、根据副账号对应的app_key得到推送地址，进行推送 3、失败，入库等JOB执行 4、成功，入库留做记录
 * 
 * @author gaoxs
 * 
 */
public class SendAccountHandleThread implements Runnable {
    private static final Logger log = core.util.Logger.getLogger(SendAccountHandleThread.class.getSimpleName());
    private IAccountExtendService AccountExtendService = null;
    private IAppExtendService appExtendService = new AppExtendServiceImpl();
    private JSONArray account = null;

    public SendAccountHandleThread(IAccountExtendService AccountExtendService, JSONArray account) {
	this.AccountExtendService = AccountExtendService;
	this.account = account;
    }

    /**
     * 线程把accounts进行处理 1、更新主cust_account的几个URL，把副账号的URL送到主的上。
     * 2、根据副账号对应的app_key得到推送地址，进行推送 3、失败，入库等JOB执行 4、成功，入库留做记录
     * 
     * @author gaoxs
     * 
     */
    public void run() {
	log.info("执行处理合并后数据" + account.toString());
	String lordcustAccount = "";// 主账号
	String url_callstatus = "";
	String url_ticket = "";
	String url_ivrdigit = "";
	// 获取主账号信息
	for (int i = 0; i < account.size(); i++) {
	    JSONObject cust = account.getJSONObject(i);
	    if (EnumUtility.isNotBlank(cust.getString("is_lord_account")) && cust.getString("is_lord_account").equals("1")) {
		lordcustAccount = cust.getString("cust_account");
	    } else {
		String cust_account = cust.getString("cust_account");
		AccountBean ViceAccountbean = AccountExtendService.queryAccountDetailByCustAccount(cust_account);
		if (EnumUtility.isNotBlank(ViceAccountbean.getUrl_callstatus())) {
		    url_callstatus = url_callstatus + ViceAccountbean.getUrl_callstatus() + ",";
		}
		if (EnumUtility.isNotBlank(ViceAccountbean.getUrl_ticket())) {
		    url_ivrdigit = url_ivrdigit + ViceAccountbean.getUrl_ticket() + ",";
		}
		if (EnumUtility.isNotBlank(ViceAccountbean.getUrl_ivrdigit())) {
		    url_ticket = url_ticket + ViceAccountbean.getUrl_ivrdigit() + ",";
		}
	    }
	}
	// 1、更新主cust_account的几个URL，把副账号的URL送到主的上。
	AccountBean lordAccountBean = AccountExtendService.queryAccountDetailByCustAccount(lordcustAccount);
	if (null != lordAccountBean) { 
	    lordAccountBean.setUrl_callstatus(EnumUtility.replaceUrl(lordAccountBean.getUrl_callstatus(), url_callstatus, "1"));
	    lordAccountBean.setUrl_ivrdigit(EnumUtility.replaceUrl(lordAccountBean.getUrl_ivrdigit(), url_ivrdigit, "1"));
	    lordAccountBean.setUrl_ticket(EnumUtility.replaceUrl(lordAccountBean.getUrl_ticket(), url_ticket, "1"));

	    // 更新主cust_account的几个URL，把副账号的URL送到主的上。
	    AccountExtendService.updateAccountExtend(lordAccountBean);
	    //
	    for (int i = 0; i < account.size(); i++) {
		JSONObject cust = account.getJSONObject(i);
		if (EnumUtility.isNotBlank(cust.getString("is_lord_account")) || cust.getString("is_lord_account").equals("0")) {
		    // 根据副账号对应的app_key得到推送地址，进行推送
		    String app_key = cust.getString("app_key");
		    String cust_account = cust.getString("cust_account");
		    this.pushAccount(app_key, cust_account, lordAccountBean);
		}
	    }
	}

    }

    /**
     * 推送信息
     * 
     * @param appkey
     *            副应用标识
     * @param cust_account
     *            副账号
     * @param bean
     *            主账号
     * @return
     */
    public int pushAccount(String appkey, String cust_account, AccountBean bean) {
	AppExtendBean appExtendBean = appExtendService.queryAppExtendByIdentify(appkey);
	String url = appExtendBean.getUrl_notice();
	// 推送参数
	JSONObject data = new JSONObject();
	data.put("describe", "账号信息");
	JSONObject object = new JSONObject();
	object.put("cust_account_vice", cust_account);
	object.put("cust_account", bean.getCust_account());
	object.put("org_tempKey", bean.getKey_private());
	object.put("org_permKey", bean.getKey_public());
	data.put("data", object);
	if (EnumUtility.isBlank(url)) {
	    log.info("推送数据因URL为空则不进行推送" + "resp:" + data.toString());
	    return 0;
	}
	// 推送数据
	log.info("推送数据开始----url=" + url + "resp:" + data.toString());
	String response = this.push(url, data);
	log.info("推送数据结束----url=" + url + "response:" + response);
	// 返回报文
	JSONObject retJson = JSONObject.fromObject(response);
	// 推送日志
	PushAccount pushData = new PushAccount();
	pushData.setCust_account(bean.getCust_account());
	pushData.setRecord_url(url);
	pushData.setVice_app_key(appkey);
	pushData.setVice_cust_account(cust_account);
	if (retJson.optString("result").equals("0")) {
	    pushData.setCode("0");// 已推送
	} else {
	    pushData.setCode("1");// 未推送,JOB半小时再次推送
	}
	int result = AccountExtendService.insertPushNotice(pushData);
	return result;
    }

    /**
     * 推送数据
     * 
     * @param url
     * @param object
     * @return
     */
    public String push(String url, JSONObject object) {
	/**
	 * 推送数据进行中
	 */
	String response = null;// 响应
	if (EnumUtility.isNotBlank(url)) {// 推送地址不能为空
	    if (url.startsWith("https")) {
		// 推送地址为https
		HttpClientCenter httpClientCenter = new HttpClientCenter(url);
		response = httpClientCenter.postHttps(object.toString(), "application/json");
	    } else if (url.startsWith("http")) {
		// 推送地址为http
		response = HttpRequest.postData(url, object.toString());
	    } else {
		log.info("推送地址非法：" + url);
	    }

	}
	return response;
    }

}
